package com.home.webapp.base.service.impl;

import com.home.common.framework.core.entity.EntityUtils;
import com.home.common.framework.core.service.impl.BaseServiceImpl;
import com.home.common.handler.exception.BizException;
import com.home.common.model.TreeNode;
import com.home.common.utils.string.StringUtils;
import com.home.webapp.base.entity.BaseRoleUser;
import com.home.webapp.base.mapper.BaseRoleUserMapper;
import com.home.webapp.base.service.BaseRoleUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Description: 用户角色 serviceImpl
 * Created by 王大宸 on 2020-10-05 23:28:38
 * Created with IntelliJ IDEA.
 */
@Service
public class BaseRoleUserServiceImpl extends BaseServiceImpl<BaseRoleUserMapper, BaseRoleUser, String> implements BaseRoleUserService {
    private static final Logger logger = LoggerFactory.getLogger(BaseRoleUserServiceImpl.class);


    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseRoleUser saveInfo(BaseRoleUser entity) throws BizException {
        try {
            BaseRoleUser roleUser = new BaseRoleUser();
            roleUser.setUserId(entity.getUserId());
            baseMapper.delete(roleUser);

            String roleIds = entity.getRoleId();
            List<String> roleList = new ArrayList<>(Arrays.asList(roleIds.split(",")));
            if (0 == roleList.size()) {
                return entity;
            }

            BaseRoleUser one = null;
            List<BaseRoleUser> list = new ArrayList<>();
            String userId = entity.getUserId();
            for (String roleId : roleList) {
                if ("1".equals(roleId)) {
                    continue;
                }
                one = new BaseRoleUser();
                one.setId(EntityUtils.getId());
                one.setUserId(userId);
                one.setRoleId(roleId);
                list.add(one);
            }
            baseMapper.batchSave(list);
            return entity;
        } catch (Exception e) {
            logger.error("给用户添加角色失败: ", e);
            throw new BizException("给用户添加角色失败");
        }
    }

    /***
     * 功能说明：获取用户角色树
     *
     * @author 王大宸
     * @date 2020/10/8 15:26
     * @param userId userId
     * @return com.home.common.framework.msg.ServerResponse<java.util.List < java.util.Map < java.lang.String, java.lang.Object>>>
     */
    @Override
    public List<TreeNode> roleTree(String userId) throws Exception {
        if (StringUtils.isEmpty(userId)) {
            throw new BizException("用户id不能为空");
        }
        try {
            BaseRoleUser roleUser = new BaseRoleUser();
            roleUser.setUserId(userId);
            List<BaseRoleUser> roleUsers = baseMapper.select(roleUser);
            StringBuilder roleIds = new StringBuilder();
            for (BaseRoleUser roleMenu : roleUsers) {
                roleIds.append(roleMenu.getRoleId()).append(",");
            }

            List<TreeNode> list = baseMapper.getTree(userId);
            for (TreeNode treeNode : list) {
                if (roleIds.toString().contains(treeNode.getId())) {
                    treeNode.setChecked(true);
                }
            }
            list.add(new TreeNode("1", "请选择角色", "", true, "1"));
            return list;
        } catch (Exception e) {
            logger.error("拉取角色列表失败:", e);
            throw new BizException("获取角色信息失败,请联系技术人员!");
        }
    }
}
